﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using AvalonDock;
using GraphSharp.Controls;

namespace BOL.Apps.GeneNetworkBrowser.Controls
{
    public class NetworkGraphLayout : GraphLayout<Gene, Relation, Network> { }

    public partial class NetworkDocument
    {
        #region Constructor

        public NetworkDocument()
        {
            InitializeComponent();
        }  

        #endregion

        #region Layout methods

        private void LayoutBFr(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.BoundedFR.ToString();
        }

        private void LayoutCircular(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.Circular.ToString();
        }

        private void LayoutEs(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.EfficientSugiyama.ToString();
        }

        private void LayoutFdp(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.CompoundFDP.ToString();
        }

        private void LayoutFr(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.FR.ToString();
        }

        private void LayoutIsom(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.ISOM.ToString();
        }

        private void LayoutKk(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.KK.ToString();
        }

        private void LayoutLl(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.LinLog.ToString();
        }

        private void LayoutTree(object sender, RoutedEventArgs e)
        {
            _networkGraphLayout.LayoutAlgorithmType = LayoutAlgorithmType.Tree.ToString();
        }

        private void VertexMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control)
            {
                var network = _networkGraphLayout.Graph;
                network.Deselect();
            }

            var gene = ((ContentPresenter)sender).Content as Gene;
            if (gene.IsSelected)
                gene.Deselect();
            else
                gene.Select();
        }

        private void EdgeMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control)
            {
                var network = _networkGraphLayout.Graph;
                network.Deselect();
            }

            var relation = ((ContentPresenter)sender).Content as Relation;
            if (relation != null && relation.IsSelected)
                relation.Deselect();
            else if (relation != null) relation.Select();

            _networkGraphLayout.MouseDown -= new MouseButtonEventHandler(NetworkGraphLayoutMouseLeftButtonDown);
        }

        private void NetworkGraphLayoutMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var network = _networkGraphLayout.Graph;
            network.Deselect();
        }

        #endregion
    }
}
